﻿using System;
using System.Collections.Generic;
using System.Text;
using PluginCore.Helpers;
using System.IO;
using System.Windows.Forms;
using ProjectManager.Projects;
using PluginCore;

namespace MUnitGenerator.Actions
{
    class CreateTestCaseAction : ActionBase
    {
        Project project;
        string filePath;

        public CreateTestCaseAction(Project project, string filePath)
        {
            this.project = project;
            this.filePath = filePath;
        }

        override protected void Execute()
        {
            string testCaseClassName = Path.GetFileNameWithoutExtension(filePath) + "Test";

            TestCaseWizard dialog = new TestCaseWizard();
            dialog.ClassName = testCaseClassName;

            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                CreateTestCase(dialog.ClassName);
                dialog.Hide();
            }
        }

        private void CreateTestCase(string className)
        {
            string testDirectoryPath = Path.Combine(project.Directory, TestPaths.RootName);
            if (!Directory.Exists(testDirectoryPath))
            {
                Directory.CreateDirectory(testDirectoryPath);
            }

            string relativePath = project.GetRelativePath(filePath);
            string subDirectory = Path.GetDirectoryName(relativePath);

            if (subDirectory.Length > TestPaths.SrcName.Length)
            {
                if (subDirectory.IndexOf(TestPaths.SrcName) == 0)
                {
                    subDirectory = subDirectory.Substring(TestPaths.SrcName.Length + 1);
                }
            }
            else
            {
                subDirectory = "";
            }

            string targetDirectory = Path.Combine(Path.Combine(testDirectoryPath, TestPaths.SrcName), subDirectory);
            if (!Directory.Exists(targetDirectory))
            {
                Directory.CreateDirectory(targetDirectory);
            }

            string text =
                "package " + subDirectory.Replace(Path.DirectorySeparatorChar, '.') + ";\n" +
                "\n" +
                "import massive.munit.Assert;\n" +
                "\n" +
                "class " + className + "\n" +
                "{\n" +
                "\tpublic function new()\n" +
                "\t{\n" +
                "\t}\n" +
                "\t\n" +
                "\t@Before\n" +
                "\tpublic function setUp():Void\n" +
                "\t{\n" +
                "\t\t\n" +
                "\t}\n" +
                "\t\n" +
                "\t@After\n" +
                "\tpublic function tearDown():Void\n" +
                "\t{\n" +
                "\t\t\n" +
                "\t}\n" +
                "\t\n" +
                "\t\n" +
                "}";

            string file = Path.Combine(targetDirectory, className + ".hx");
            File.WriteAllText(file, text);
            PluginBase.MainForm.OpenEditableDocument(file);
        }
    }
}
